package com.WebProject.web;

import com.WebProject.pojo.t_user;
import com.WebProject.service.LoginDao.IloginDao;
import com.WebProject.service.LoginDao.Impl.IloginDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @PACKAGE_NAME: com.WebProject.Control
 * @NAME: CheckUser
 * @USER: ZhouYongCheng
 * @DATE: 2022/7/29
 * @TIME: 19:52
 * @Description: Web检查用户名或密码是否正确
 */
public class CheckUser extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        IloginDao loginDao = new IloginDaoImpl();

        t_user login = loginDao.login(username);

        if (login != null && username.equals(login.getUsername()) && password.equals(login.getPassword())){

            HttpSession session = req.getSession();
            if (login.getType() == 1){
                session.setAttribute("username",username);
                req.getRequestDispatcher("/pages/AdminPage/loginsuccess.jsp").forward(req,resp);
            }else {
                session.setAttribute("username",username);
                req.getRequestDispatcher("/pages/UserPage/loginsuccess.jsp").forward(req,resp);
            }
        }
        req.setAttribute("msg","用户名或密码错误");
        req.getRequestDispatcher("index.jsp").forward(req,resp);
    }
}
